package com.yidont.douyinhelper.ui

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.Check
import androidx.compose.material.icons.filled.ToggleOff
import androidx.compose.material.icons.filled.ToggleOn
import androidx.compose.material.icons.outlined.ToggleOff
import androidx.compose.material.icons.outlined.ToggleOn
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import com.yidont.douyinhelper.data.configData
import com.yidont.douyinhelper.data.followBean
import com.yidont.douyinhelper.util.isAccessibilitySettingsOn
import com.yidont.douyinhelper.util.launchApp
import com.yidont.douyinhelper.util.startAccessibilitySettings

/**
 * 关注、吸粉
 */
@Composable
fun ConcernPage(back: () -> Unit) {
    var enabled by remember { mutableStateOf(isAccessibilitySettingsOn()) }
    val owner = LocalLifecycleOwner.current
    DisposableEffect(owner) {
        val observer = LifecycleEventObserver { _, event ->
            if (event == Lifecycle.Event.ON_RESUME) {
                enabled = isAccessibilitySettingsOn()
            }
        }
        owner.lifecycle.addObserver(observer)
        onDispose { owner.lifecycle.removeObserver(observer) }
    }
    Scaffold(Modifier.fillMaxSize(), topBar = { TopBar(back) }) {
        Column(
            Modifier
                .padding(it)
                .padding(16.dp)
                .fillMaxSize()
        ) {
            ActionTime()
            CommentText()
            SkipConfigs()
            AccessibilityText(enabled)
            StartRun(enabled)
        }
    }
}

@Composable
private fun TopBar(back: () -> Unit) {
    TopAppBar(title = { Text(text = "智能关注") }, Modifier.fillMaxWidth(), navigationIcon = {
        IconButton(
            onClick = back
        ) {
            Icon(Icons.Default.ArrowBack, null)
        }
    })
}

@Composable
private fun ActionTime() {
    var text by remember { mutableStateOf(configData.actionDelay.toString()) }
    TextField(
        value = text,
        onValueChange = {
            text = it
            text.toLongOrNull()?.let { time ->
                if (time < 1000) {
                    configData.actionDelay = 1000
                } else {
                    configData.actionDelay = time
                }
            }
        },
        label = { Text(text = "执行操作的时间间隔(毫秒)") },
        keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
    )
}

@Composable
private fun CommentText() {
    var text by remember { mutableStateOf(followBean.comment) }
    TextField(
        value = text,
        onValueChange = {
            text = it
            followBean.comment = text
        },
        label = { Text(text = "评论内容") },
        keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Text),
    )
}

@Composable
private fun SkipConfigs() {
    var skip1 by remember { mutableStateOf(true) }
    IconToggleButton(checked = skip1, onCheckedChange = {
        skip1 = it
        followBean.skipVideo0 = skip1
    }) {
        Row(verticalAlignment = Alignment.CenterVertically) {
            Text(text = "跳过0作品")
            Icon(
                if (skip1) Icons.Filled.ToggleOn else Icons.Outlined.ToggleOff, null,
                tint = if (skip1) MaterialTheme.colors.primary else LocalContentColor.current
            )
        }
    }
    var skip2 by remember { mutableStateOf(true) }
    IconToggleButton(checked = skip2, onCheckedChange = {
        skip2 = it
        followBean.skipPrivacy = skip2
    }) {
        Row(verticalAlignment = Alignment.CenterVertically) {
            Text(text = "跳过私密账号")
            Icon(
                if (skip2) Icons.Filled.ToggleOn else Icons.Outlined.ToggleOff, null,
                tint = if (skip2) MaterialTheme.colors.primary else LocalContentColor.current
            )
        }
    }
}

@Composable
private fun AccessibilityText(enabled: Boolean) {
    if (enabled) {
        Text(text = "无障碍服务已开启")
    } else {
        Row(
            Modifier.fillMaxWidth(),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text(text = "无障碍服务未开启")
            Button(onClick = { startAccessibilitySettings() }, Modifier.padding(start = 12.dp)) {
                Text(text = "开启无障碍")
            }
        }
    }
}

@Composable
private fun StartRun(enabled: Boolean) {
    Button(onClick = {
        configData.showLogWindow.value = true
        launchApp()
    }, enabled = enabled) {
        Text(text = "开始运行")
    }
}